<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>隔行变色</title>
  <!-- IMPORT CSS -->
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    ul {
      list-style: none;
    }
    #newsBox {
      box-sizing: border-box;
      width: 300px;
      padding: 20px;
      margin: 20px auto;
      border: 2px solid lightblue;
    }
    #newsBox li {
      line-height: 50px;
      border-bottom: 1px dashed lightcoral;
    }
    #newsBox li:nth-last-child(1) {
      border-bottom: none;
    }
    /* 基于CSS实现 */
    /* #newsBox li:nth-child(even) {
      background: lightgray;
    }
    #newsBox li:hover {
      background: lightgreen;
    } */
  </style>
</head>
<body>
  <ul id="newsBox">
    <li>我是第1个li</li>
    <li>我是第2个li</li>
    <li>我是第3个li</li>
    <li>我是第4个li</li>
    <li>我是第5个li</li>
  </ul>
  
  <!-- IMPORT JS -->
  <script>
    let newsBox = document.getElementById('newsBox');
    let newsList = newsBox.getElementsByTagName('li');
    for(let i = 0; i < newsList.length; i++) {
      newsList[i].style.backgroundColor = i % 2 === 0 ? '#FFF' : 'red';

      // 循环的时候给每一个LI都绑定moseover/mouseout事件
      newsList[i].onmouseover = function() {
        // this : 当前操作的这个LI
        this.style.backgroundColor = "lightblue";
      }

      newsList[i].onmouseout = function() {
        this.style.backgroundColor = '#fff'
      }
    }
  </script>
</body>
</html>